import PageHeaderWrapper from "@/components/PageHeaderWrapper";
import PageLoading from "@/components/PageLoading";
import { Card, Dropdown, Form, Icon, Input, Layout, Menu, Modal, Pagination, Spin, Table, Tree, message } from "antd";
import React, { Suspense } from "react";
import moment from "moment";
import {ButtonGroup, ImportModal} from "@/utils/CommonComponent";
import { selfBuiltList, selfBuiltListDelete, selfBuiltListEdit, selfBuiltPasswordReset, selfBuiltTree, selfBuiltTreeAdd, selfBuiltTreeDelete, selfBuiltTreeEdit } from "@/services/aigcUser";
import tableDataCheck from "@/utils/isBackPrevPage";
import { adminAccountlist } from "@/services/tenantManagement";
import UserUpdate from "./userUpdate";

class AdminAccount extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      leftLoading:false,
      leftData:[],
      overlayVisible:false,
      currentNode:{},
      modalType:"",
      modalVisible:false,
      optNode:{},
      selectedKeys:[],
      expandedKeys:[],
      list:[],
      current:1,
      size:10,
      categoryId:'',
      name:"",
      loading:false,
      importVisible:false,
      importLoading:false
    }
  }

  componentDidMount(){
    this.getTableData()
  }

  doSearch = (current,size) => {
    this.setState({
      current,
      size
    },() => {
      this.getTableData()
    })
  }

  getTableData = () => {
    this.setState({
      loading:true
    },async () => {
      const { current,size,name } = this.state
      const res = await adminAccountlist({
        current,
        size,
        name,
      })

      this.setState({
        loading:false,
        list:res.records,
        total:res.total
      })
    })
  }

  openUpdateModal = id => {
    const title = id ? '编辑用户' : '新增用户'
    this.UserUpdate.doInit(id,title)
  }

  resetConfirm = (id) => {
    const that = this;
    Modal.confirm({
      title: '确定重置密码吗？',
      okText: '确定',
      cancelText: '取消',
      okType: 'danger',
      onOk: () => {
        that.doReset(id)
      }
    })
  }

  doReset = async id => {
    const res = await selfBuiltPasswordReset({id})
    message.info(res.data)
  }

  startOrStop = async row => {
    const res = await selfBuiltListEdit({id:row.id,disabled:!row.disabled})
    message.info(res.message)
    this.getTableData()
  }

  removeItemConfirm = (id) => {
    Modal.confirm({
      title:"是否确定要删除？",
      okText:"确定",
      cancelText:"取消",
      okType:"danger",
      onOk:() => {
        this.doItemConfirm(id)
      }
    })
  }

  doItemConfirm = async id => {
    const { list,current,size } = this.state;
    const res = await selfBuiltListDelete({id})
    message.info(res.message)
    const newCurrent = tableDataCheck(list, current)
    this.doSearch(newCurrent,size)
  }

  generateRandom() {
    return Math.floor(Math.random() * 9000) + 1000;
  }

  render(){
    const { 
      leftLoading,
      leftData,
      modalType,
      modalVisible,
      selectedKeys,
      optNode,
      currentNode,
      expandedKeys,
      name,
      current,
      size,
      list,
      loading,
      total,
      importVisible,
      importLoading 
    } = this.state
    const { form:{getFieldDecorator} } = this.props;

    const columns = [
			{
				title: "用户姓名",
				dataIndex: "nickName",
				width: 200,
			},
      {
				title: "邮箱",
				dataIndex: "email",
				width: 200,
			},
			{
				title: "登录用户名",
				dataIndex: "userName",
				width: 120,
			},
			{
				title: '创建时间',
				dataIndex: 'createTime',
				key: 'createTime',
				width: 150,
				render: (createTime) => {
					if (createTime) {
						return moment(createTime).format('YYYY-MM-DD hh:mm:ss')
					} else {
						return createTime;
					}
				}
			},
			{
				title: "操作",
				width: 200,
				render: (text, row) => {
          return(
            <div>
              <ButtonGroup 
                title="编辑"
                group="edit-table"
                handleClick={() => this.openUpdateModal(row.id)}
              />
              <ButtonGroup 
                title="重置密码"
                group="reset-table"
                handleClick={() => this.resetConfirm(row.id)}
              />
              <ButtonGroup 
                title="删除"
                group="delete-table"
                handleClick={() => this.removeItemConfirm(row.id)}
                style={{marginRight:0}}
              />
            </div>
          )
				},
			},
		];
    return (
      <Suspense fallback={<PageLoading />}>
        <PageHeaderWrapper>
          <Card bordered={false} style={{minHeight:"100%"}}>
            <div className="topOptionBox">
              <Input.Search 
                placeholder="请输入登录用户名" 
                value={name} 
                onChange={(e) => this.setState({name:e.target.value})} 
                onSearch={() => this.doSearch(1,size)} 
                style={{width:200}}
              />
              <div style={{flexShrink:'0'}}>
                <ButtonGroup 
                  title="新增用户"
                  group="add"
                  handleClick={() => this.openUpdateModal()}
                  style={{marginRight:0}}
                />
              </div>
            </div>
            <Table 
              dataSource={list}
              loading={loading}
              rowKey='id'
              pagination={false}
              scroll={{ x: "auto", y: 'calc(100vh - 300px)' }}
              columns={columns}
            />
            {parseInt(total) > 10 ? (
              <Pagination
                showQuickJumper
                total={total}
                current={current}
                onChange={this.doSearch}
                style={{ textAlign: 'right', marginTop: 16 }}
              />
            ) : null}
          </Card>

          <UserUpdate 
            onRef={ref => this.UserUpdate = ref}
            doRefresh={() => this.doSearch(1,size)}
          />
        </PageHeaderWrapper>
      </Suspense>
    )
  }
}

export default Form.create()(AdminAccount)